﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class VendorInvoiceDetail : System.Web.UI.Page
    {
        VendorInvoiceController vendorInvoiceController=new VendorInvoiceController();
        StockInController stockInController=new StockInController();
        StorageController storageController=new StorageController();
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request.QueryString["vendInvoiceId"]!=null)
            {
                try
                {
                    EditButton.Visible = false;
                    int vendorInvoiceId = Convert.ToInt32(Request.QueryString["vendInvoiceId"]);
                    VendorInvoiceEntity vendorInvoiceEntity =
                        vendorInvoiceController.FindVendorInvoiceById(vendorInvoiceId);

                    InvoiceNoLable.Text = vendorInvoiceEntity.VendorInvoiceNo;
                    VendorLabel.Text = vendorInvoiceEntity.VendorShortName;
                    PaidLabel.Text = vendorInvoiceEntity.Paid;
                    DateLabel.Text = vendorInvoiceEntity.VendorInvoiceDate.ToString("d MMM, yyyy");

                    IList<StorageEntity> storageEntities = storageController.FindStorageByInvoiceId(vendorInvoiceId);
                    VendorInvoiceItemsGridView.DataSource = storageEntities;
                    VendorInvoiceItemsGridView.DataBind();
                }
                catch(Exception)
                {
                    //Session["Massage"] = "No such vendor invoice.";
                    Session["FlashMessage"] = "Error: No Such Vendor Invoice!";
                    Session["FlashMessageType"] = "error";
                    Response.Redirect("VendorInvoiceList.aspx",false);
                }
            }
            else
            {
                Response.Redirect("VendorInvoiceList.aspx");
            }
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }

        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("EditVendorInvoice.aspx?vendorInvoiceId=" + Request.QueryString["vendInvoiceId"]);
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            int vendorInvoiceId = Convert.ToInt32(Request.QueryString["vendInvoiceId"]);
            int msg=vendorInvoiceController.DeleteVendorInvoiceById(vendorInvoiceId);
            if(msg==1)
            {
                //this.MsgLabel.Text = "Delete successful!";
                Session["FlashMessage"] = "Success: Delete Successful!";
                Session["FlashMessageType"] = "success";
            }
            if(msg==0)
            {
                //this.MsgLabel.Text = "Delete unsuccessful!";
                Session["FlashMessage"] = "Error: Delete Unsuccessful!";
                Session["FlashMessageType"] = "error";
            }
        }
    }
}